/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-12-05
 * Time: 14:04
 */
public class TestDemo {

    class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;





    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        Node cur=searchPlace(key);
        Node del=cur.next;
        cur.next=del.next;

    }
    private Node searchPlace(int key){
        Node cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }





    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data){
        Node node=new Node(data);
        Node cur=findPlace(index);
        node.next=cur.next;
        cur.next=node;


    }




    private  void isValid(int index) throws  IndexOutOfException{
        if(index<0||index>size()){
            throw new IndexOutOfException("index位置不合法");

        }


    }

    private Node findPlace(int index){
        Node cur=head;
        int count=0;
        while(count!=index-1){
            count++;
            cur=cur.next;
        }
        return cur;
    }
}
